﻿
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;

namespace IPBoss.Copyright.UtilityHelper
{
    public class ImageHelper
    {
        protected Log4NetHelper LogHelper { get { return Log4NetHelper.Create(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); } }
        ///
        /// 将内存流转为图片
        ///
        /// 
        /// 
        public static Image GetFile(string path)
        {
            try
            {

                MemoryStream stream = ReadFile(path);
                return stream == null ? null : Image.FromStream(stream);

            }
            catch (Exception ex)
            {
                Log4NetHelper.Error("ImageHelper", "IPBoss.MIS.UtilityHelper.GetFile," + path + ex.Message);
                return null;
            }
        }
        ///
        /// 将文件转为内存流
        ///
        /// 
        /// 
        public static MemoryStream ReadFile(string path)
        {
            if (!File.Exists(path))
            {
                 Log4NetHelper.Error("ImageHelper", "IPBoss.MIS.UtilityHelper.ReadFile,文件不存在");
                return null;
            }
            using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                byte[] b = new byte[file.Length];
                file.Read(b, 0, b.Length);
                MemoryStream stream = new MemoryStream(b);
                return stream;
            }
        }
    }
}
